home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / ctutor.zip / CHAP3.TXT < prev    next >
Text File  |  1987-07-04  |  19KB  |  454 lines

  1.                         Chapter 3 - Program Control
  2.  
  3.  
  4.                                THE WHILE LOOP
  5.  
  6.              The  C programming language has several structures  for
  7.         looping  and conditional branching.   We will cover them all
  8.         in this chapter and we will begin with the while loop.   The
  9.         while  loop continues to loop while some condition is  true.
  10.         When   the   condition  becomes  false,   the   looping   is
  11.         discontinued.   It therefore does just what it says it does,
  12.         the name of the loop being very descriptive.
  13.  
  14.              Load the program WHILE.C and display it for an  example
  15.         of  a while loop.   We begin with a comment and the  program
  16.         name,  then  go  on  to define an integer  variable  "count"
  17.         within the body of the program.  The variable is set to zero
  18.         and we come to the while loop itself.  The syntax of a while
  19.         loop is just as shown here.  The keyword "while" is followed
  20.         by an expression of something in parentheses,  followed by a
  21.         compound  statement  bracketed by braces.   As long  as  the
  22.         expression in parenthesis is true, all statements within the
  23.         braces will be executed.   In this case,  since the variable
  24.         count  is incremented by one every time the  statements  are
  25.         executed, it will eventually reach 6, the statement will not
  26.         be executed,  and the loop will be terminated.   The program
  27.         control   will   resume  at  the  statement  following   the
  28.         statements in braces.
  29.  
  30.              We  will  cover  the compare  expression,  the  one  in
  31.         parentheses, in the next chapter.  Until then, simply accept
  32.         the  expressions for what you think they should do  and  you
  33.         will probably be correct.
  34.  
  35.              Several  things must be pointed out regarding the while
  36.         loop.   First,  if the variable count were initially set  to
  37.         any  number greater than 5,  the statements within the  loop
  38.         would  not be executed at all,  so it is possible to have  a
  39.         while  loop  that  never  is  executed.   Secondly,  if  the
  40.         variable  were  not incremented in the loop,  then  in  this
  41.         case,  the loop would never terminate, and the program would
  42.         never complete.   Finally, if there is only one statement to
  43.         be executed within the loop, it does not need braces but can
  44.         stand alone.
  45.  
  46.              Compile and run this program.
  47.  
  48.                              THE DO-WHILE LOOP
  49.  
  50.              A  variation  of the while loop is illustrated  in  the
  51.         program DOWHILE.C,  which you should load and display.  This
  52.         program  is nearly identical to the last one except that the
  53.         loop  begins  with the reserved word  "do",  followed  by  a
  54.         compound  statement  in  braces,   then  the  reserved  word
  55.  
  56.  
  57.                                  Page 13
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                         Chapter 3 - Program Control
  68.  
  69.  
  70.         "while",  and  finally  an expression in  parentheses.   The
  71.         statements in the braces are executed repeatedly as long  as
  72.         the expression in parentheses is true.   When the expression
  73.         in parentheses becomes false,  execution is terminated,  and
  74.         control passes to the statements following this statement.
  75.  
  76.              Several  things  must  be pointed  out  regarding  this
  77.         statement.  Since  the test is done at the end of the  loop,
  78.         the  statements  in  the braces will always be  executed  at
  79.         least once.   Secondly,  if "i" were not changed within  the
  80.         loop,  the loop would never terminate, and hence the program
  81.         would  never terminate.   Finally,  just like for the  while
  82.         loop,  if  only  one statement will be executed  within  the
  83.         loop,  no braces are required.  Compile and run this program
  84.         to see if it does what you think it should do.
  85.  
  86.              It  should come as no surprise to you that these  loops
  87.         can be nested.  That is, one loop can be included within the
  88.         compound  statement of another loop,  and the nesting  level
  89.         has no limit.
  90.  
  91.                                 THE FOR LOOP
  92.  
  93.              The  "for" loop is really nothing new,  it is simply  a
  94.         new  way  to describe the "while" loop.   Load and edit  the
  95.         file  named  FORLOOP.C for an example of a  program  with  a
  96.         "for"  loop.  The  "for" loop consists of the reserved  word
  97.         "for" followed by a rather large expression in  parentheses.
  98.         This expression is really composed of three fields separated
  99.         by  semi-colons.   The  first field contains the  expression
  100.         "index  = 0" and is an initializing field.   Any expressions
  101.         in  this field are executed prior to the first pass  through
  102.         the loop.   There is essentially no limit as to what can  go
  103.         here,  but  good programming practice would require it to be
  104.         kept simple.   Several initializing statements can be placed
  105.         in this field, separated by commas.
  106.  
  107.              The second field,  in this case containing "index < 6",
  108.         is  the  test which is done at the beginning  of  each  loop
  109.         through  the program.   It can be any expression which  will
  110.         evaluate  to a true or false.   (More will be said about the
  111.         actual value of true and false in the next chapter.)
  112.  
  113.              The expression contained in the third field is executed
  114.         each time the loop is executed but it is not executed  until
  115.         after  those  statements  in the main body of the  loop  are
  116.         executed.   This field, like the first, can also be composed
  117.         of several operations separated by commas.
  118.  
  119.              Following  the  for()  expression  is  any  single   or
  120.         compound statement which will be executed as the body of the
  121.  
  122.  
  123.                                  Page 14
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                         Chapter 3 - Program Control
  134.  
  135.  
  136.         loop.   A  compound  statement  is  any  group  of  valid  C
  137.         statements enclosed in braces.   In nearly any context in C,
  138.         a  simple statement can be replaced by a compound  statement
  139.         that will be treated as if it were a single statement as far
  140.         as program control goes.  Compile and run this program.
  141.  
  142.              You  may  be  wondering why there  are  two  statements
  143.         available that do exactly the same thing because the "while"
  144.         and  the "for" loop do exactly the same thing.  The  "while"
  145.         is convenient to use for a loop that you don't have any idea
  146.         how many times the loop will be executed, and the "for" loop
  147.         is  usually used in those cases when you are doing  a  fixed
  148.         number  of  iterations.  The "for" loop is  also  convenient
  149.         because  it moves all of the control information for a  loop
  150.         into one place, between the parentheses, rather than at both
  151.         ends  of the code.  It is your choice as to which you  would
  152.         rather use.
  153.  
  154.                               THE IF STATEMENT
  155.  
  156.              Load  and  display the file IFELSE.C for an example  of
  157.         our first conditional branching statement, the "if".  Notice
  158.         first,  that there is a "for" loop with a compound statement
  159.         as its executable part containing two "if" statements.  This
  160.         is an example of how statements can be nested.  It should be
  161.         clear  to  you  that each of the  "if"  statements  will  be
  162.         executed 10 times.
  163.  
  164.              Consider the first "if" statement.   It starts with the
  165.         keyword  "if" followed by an expression in parentheses.   If
  166.         the expression is evaluated and found to be true, the single
  167.         statement following the "if" is executed,  and if false, the
  168.         following  statement  is  skipped.   Here  too,  the  single
  169.         statement  can be replaced by a compound statement  composed
  170.         of  several statements bounded by  braces.   The  expression
  171.         "data  == 2" is simply asking if the value of data is  equal
  172.         to 2,  this will be explained in detail in the next chapter.
  173.         (Simply suffice for now that if "data = 2" were used in this
  174.         context, it would mean a completely different thing.)
  175.  
  176.                             NOW FOR THE IF-ELSE
  177.  
  178.              The  second  "if"  is  similar to the  first  with  the
  179.         addition  of a new reserved word,  the "else" following  the
  180.         first  printf  statement.   This  simply says  that  if  the
  181.         expression in the parentheses evaluates as true,  the  first
  182.         expression  is executed,  otherwise the expression following
  183.         the "else" is executed.   Thus,  one of the two  expressions
  184.         will  always be executed,  whereas in the first example  the
  185.         single expression was either executed or skipped.  Both will
  186.  
  187.  
  188.  
  189.                                  Page 15
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                         Chapter 3 - Program Control
  200.  
  201.  
  202.         find  many uses in your C programming efforts.   Compile and
  203.         run this program to see if it does what you expect.
  204.  
  205.                            THE BREAK AND CONTINUE
  206.  
  207.              Load  the file named BREAKCON.C for an example  of  two
  208.         new statements.  Notice that in the first "for", there is an
  209.         if  statement that calls a break if xx equals 8.   The break
  210.         will  jump  out of the loop you are in and  begin  executing
  211.         statements following the loop,  effectively terminating  the
  212.         loop.   This  is a valuable statement when you need to  jump
  213.         out  of  a  loop  depending on the  value  of  some  results
  214.         calculated  in the loop.   In this case,  when xx reaches 8,
  215.         the  loop is terminated and the last value printed  will  be
  216.         the previous value, namely 7.
  217.  
  218.              The  next  "for" loop,  contains a  continue  statement
  219.         which  does not cause termination of the loop but jumps  out
  220.         of the present iteration.  When the value of xx reaches 8 in
  221.         this case,  the program will jump to the end of the loop and
  222.         continue  executing  the loop,  effectively eliminating  the
  223.         printf statement during the pass through the loop when xx is
  224.         eight.   Compile and run the program to see if it does  what
  225.         you expect.
  226.  
  227.                             THE SWITCH STATEMENT
  228.  
  229.              Load  and  display the file SWITCH.C for an example  of
  230.         the  biggest construct yet in the C  language,  the  switch.
  231.         The switch is not difficult, so don't let it intimidate you.
  232.         It  begins with the keyword "switch" followed by a  variable
  233.         in parentheses which is the switching variable, in this case
  234.         "truck".   As many cases as desired are then enclosed within
  235.         a pair of braces.  The reserved word "case" is used to begin
  236.         each  case  entered followed by the value of  the  variable,
  237.         then a colon, and the statements to be executed.
  238.  
  239.              In  this example,  if the variable "truck" contains the
  240.         value 3 during this pass of the switch statement, the printf
  241.         will  cause "The value is three" to be  displayed,  and  the
  242.         "break" statement will cause us to jump out of the switch.
  243.  
  244.              Once  an  entry  point is  found,  statements  will  be
  245.         executed until a "break" is found or until the program drops
  246.         through  the bottom of the switch braces.   If the  variable
  247.         has  the value 5,  the statements will begin executing where
  248.         "case  5  :" is found,  but the first statements  found  are
  249.         where the case 8 statements are.  These are executed and the
  250.         break  statement  in the "case 8" portion  will  direct  the
  251.         execution  out the bottom of the switch.   The various  case
  252.  
  253.  
  254.  
  255.                                  Page 16
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                         Chapter 3 - Program Control
  266.  
  267.  
  268.         values can be in any order and if a value is not found,  the
  269.         default portion of the switch will be executed.
  270.  
  271.              It should be clear that any of the above constructs can
  272.         be  nested  within  each  other  or  placed  in  succession,
  273.         depending on the needs of the particular programming project
  274.         at hand.
  275.  
  276.              Compile  and  run SWITCH.C to see if it does  what  you
  277.         expect it to after this discussion.
  278.  
  279.              Load  and display the file GOTOEX.C for an example of a
  280.         file  with some "goto" statements in it.   To use  a  "goto"
  281.         statement,  you simply use the reserved word "goto" followed
  282.         by the symbolic name to which you wish to jump.  The name is
  283.         then  placed  anywhere in the program followed by  a  colon.
  284.         You  are  not  allowed to jump into any loop,  but  you  are
  285.         allowed to jump out of a loop.  Also, you are not allowed to
  286.         jump out of any function into another.   These attempts will
  287.         be  flagged  by  your Turbo C compiler as an  error  if  you
  288.         attempt any of them.
  289.  
  290.              This  particular program is really a mess but it  is  a
  291.         good example of why software writers are trying to eliminate
  292.         the  use of the "goto" statement as much as  possible.   The
  293.         only place in this program where it is reasonable to use the
  294.         "goto"  is the one in line 17 where the program jumps out of
  295.         the three nested loops in one jump.   In this case it  would
  296.         be  rather messy to set up a variable and jump  successively
  297.         out of all three loops but one "goto" statement gets you out
  298.         of all three.
  299.  
  300.              Some  persons say the "goto" statement should never  be
  301.         used  under  any  circumstances but this  is  rather  narrow
  302.         minded  thinking.   If there is a place where a "goto"  will
  303.         clearly  do a neater control flow than some other construct,
  304.         feel free to use it.  It should not be abused however, as it
  305.         is in the rest of the program on your monitor.
  306.  
  307.              Entire  books  are written on  "gotoless"  programming,
  308.         better known as Structured Programming.   These will be left
  309.         to  your  study.   One  point  of reference  is  the  Visual
  310.         Calculator described in Chapter 14 of this  tutorial.   This
  311.         program  is  contained in four separately compiled  programs
  312.         and  is a rather large complex program.   If you spend  some
  313.         time studying the source code,  you will find that there  is
  314.         not  a single "goto" statement anywhere in it.   Compile and
  315.         run  GOTOEX.C  and study its output.   It would  be  a  good
  316.         exercise  to rewrite it and see how much more readable it is
  317.         when the statements are listed in order.
  318.  
  319.  
  320.  
  321.                                  Page 17
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.                         Chapter 3 - Program Control
  332.  
  333.  
  334.                        FINALLY, A MEANINGFUL PROGRAM
  335.  
  336.              Load  the  file  named TEMPCONV.C for an example  of  a
  337.         useful,  even  though somewhat limited program.   This is  a
  338.         program  that generates a list of centigrade  and fahrenheit
  339.         temperatures  and prints a message out at the freezing point
  340.         of water and another at the boiling point of water.
  341.  
  342.              Of particular importance is the formatting.  The header
  343.         is  simply  several lines of comments  describing  what  the
  344.         program  does in a manner that catches the readers attention
  345.         and  is  still  pleasing to the  eye.  You  will  eventually
  346.         develop your own formatting style, but this is a good way to
  347.         start.   Also if you observe the for loop,  you will  notice
  348.         that  all  of  the contents of the  compound  statement  are
  349.         indented  3 spaces to the right of the "for" reserved  word,
  350.         and  the  closing brace is lined up under the "f" in  "for".
  351.         This  makes debugging a bit easier because the  construction
  352.         becomes  very  obvious.   You  will  also  notice  that  the
  353.         "printf"  statements that are in the "if" statements  within
  354.         the  big  "for" loop are indented  three  additional  spaces
  355.         because they are part of another construct.
  356.  
  357.              This  is  the first program in which we used more  than
  358.         one  variable.   The three variables are simply  defined  on
  359.         three  different lines and are used in the same manner as  a
  360.         single variable was used in previous programs.   By defining
  361.         them  on different lines,  we have an opportunity to  define
  362.         each with a comment.
  363.  
  364.                       ANOTHER POOR PROGRAMMING EXAMPLE
  365.  
  366.              Recalling  UGLYFORM.C from the last chapter,  you saw a
  367.         very  poorly  formatted program.   If you load  and  display
  368.         DUMBCONV.C you will have an example of poor formatting which
  369.         is  much closer to what you will actually find in  practice.
  370.         This  is  the same program as TEMPCONV.C with  the  comments
  371.         removed  and  the  variable  names  changed  to  remove  the
  372.         descriptive aspect of the names.  Although this program does
  373.         exactly the same as the last one,  it is much more difficult
  374.         to  read and understand.   You should begin to develop  good
  375.         programming practices now.
  376.  
  377.              Compile  and  run  this program to  see  that  it  does
  378.         exactly what the last one did.
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.                                  Page 18
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.                         Chapter 3 - Program Control
  398.  
  399.  
  400.         PROGRAMMING EXERCISES
  401.  
  402.         1.  Write a program that writes your name on the monitor ten
  403.             times.   Write this program three times, once with  each
  404.             looping method.
  405.  
  406.         2.  Write a program that counts from one to ten, prints the
  407.             values  on  a separate line for each,  and  includes  a
  408.             message  of  your  choice when the count  is  3  and  a
  409.             different message when the count is 7.
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.                                  Page 19
  454.